package com.qdlc.p2p.biz.processor.task;

import java.text.MessageFormat;

import javax.annotation.Resource;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.qdlc.p2p.biz.executer.impl.NewActivityAwardExecutor;
import com.qdlc.p2p.biz.processor.event.LotteryValueEvent;
import com.qdlc.p2p.biz.service.lottery.LotteryTaskService;
import com.qdlc.p2p.common.constant.LotteryCashType;
import com.qdlc.p2p.common.constant.LotteryType;
import com.qdlc.p2p.common.core.queue.TaskProcessorException;
import com.qdlc.p2p.common.core.queue.ValueEvent;
import com.qdlc.p2p.common.util.LogFactory;
import com.qdlc.p2p.dal.dto.User;

/**
 * Created by hongbl on 2016/2/2.
 * 抽奖Processor
 */
@Service
public class LotteryTaskProcessor extends AbstractTaskProcessor {

    //抽奖日志先暂时放在额外日志
    private static final Logger logger = LogFactory.EXCEPTION_LOGGER;

    @Resource
    LotteryTaskService lotteryTaskService;

    @Override
    protected void process0(ValueEvent event) throws TaskProcessorException {
        LotteryValueEvent lotteryEvent = (LotteryValueEvent)event;
        //获取抽奖类型
        int resultType = lotteryEvent.getLotteryResult().getType();
        //获奖价值
        double amount = lotteryEvent.getLotteryResult().getValue();
        //获取用户
        User user =  lotteryEvent.getUser();
        long userId =  user.getUserId();
        String message = MessageFormat.format("用户:{0}抽中奖品开始赠送,类型为{1}",userId, resultType);
        logger.info(message);
        if (resultType == LotteryType.TYPE_SCORE) {
            lotteryTaskService.giveScore(userId,amount);
        } else if (resultType == LotteryType.TYPE_MONEY) {
            lotteryTaskService.giveCash(user,amount, LotteryCashType.NORMAL);
        } else {
            NewActivityAwardExecutor.give(resultType, lotteryEvent.getBaseGiveAwardModel());
        }
    }

    @Override
    public boolean supports(ValueEvent event) {
        return event instanceof LotteryValueEvent;
    }

}
